﻿@using System.Web.Optimization
@using VocaDb.Model
@using VocaDb.Model.DataContracts.Tags
@using VocaDb.Model.Domain
@using Res = ViewRes.Tag.DetailsStrings;
@using VocaDb.Model.Domain.Images
@using VocaDb.Model.Domain.Security
@using VocaDb.Model.Domain.Tags
@using VocaDb.Web.Helpers;
@using VocaDb.Web.Models.Shared
@using SharedRes = ViewRes.SharedStrings;
@inherits VocaDb.Web.Code.VocaDbPage<TagDetailsContract>

@{

	ViewBag.Parents = new[] { Html.ActionLink(SharedRes.Tags, "Index") };

	var smallThumbUrl = Url.EntryImageOld(Model.Thumb, ImageSize.SmallThumb);
	var thumbUrl = Url.EntryImageOld(Model.Thumb, ImageSize.Original);

	var url = PageProperties.CanonicalUrl;

	var related = new List<Tuple<TagBaseContract, string>>();

	if (Model.Parent != null) {
		related.Add(Tuple.Create(Model.Parent, "parent"));
	}

	related.AddRange(Model.Children.Select(t => Tuple.Create(t, "child")));
	related.AddRange(Model.Siblings.Select(t => Tuple.Create(t, "sibling")));

}

@section Head {
	@HtmlHelpers.OpenGraphMetaTags(PageProperties)
	<link rel="canonical" href="@url" />
}

@section BodyScripts {
	<script src="https://code.highcharts.com/highcharts.js" type="text/javascript"></script>
	@Scripts.Render("~/bundles/Tag/Details")
	<script type="text/javascript">

		$(function () {

			var urlMapper = new vdb.UrlMapper('@UrlMapper.HostAddress');
			var jsonModel = @ToJS(new { Model.Name, Model.Parent, Model.Children, Model.Siblings });
			var vm;

			initChart(urlMapper, jsonModel.name, jsonModel.parent, jsonModel.siblings, jsonModel.children);

			var repo = new vdb.repositories.TagRepository('@RootPath');
			var userRepo = new vdb.repositories.UserRepository(urlMapper);
			var latestComments = @ToJS(Model.LatestComments);
			var canDeleteAllComments = @ToJS(UserContext.HasPermission(PermissionToken.DeleteComments));
			var reportTypes = @ToJS(ReportUtils.GetReportTypes(Translate.TagReportTypeNames, TagReport.ReportTypesWithRequiredNotes));

			vm = new vdb.viewModels.tags.TagDetailsViewModel(repo, userRepo, latestComments, reportTypes, @UserContext.LoggedUserId, @Model.Id, canDeleteAllComments,
				@ToJS(Model.Description.ShowEnglish(UserContext.LanguagePreference)), @ToJS(Model.IsFollowing));
			ko.applyBindings(vm);
			initTagsPage(vm);

		});

	</script>
}

@section Toolbar {
	<a href="#" data-bind="click: followTag, jqButton: { icon: 'ui-icon-plus' }, visible: !isFollowed(), disable: !isLoggedIn">@Res.FollowTag</a>
	<a href="#" data-bind="click: unfollowTag, jqButton: { icon: 'ui-icon-close' }, visible: isFollowed()">@Res.UnfollowTag</a>

	@Html.ActionLink(ViewRes.SharedStrings.Edit, "Edit", new { id = Model.Id }, new { id = "editTagLink", @class = (Login.CanEdit(Model) ? "" : "disabled") })
	@Html.ActionLink(ViewRes.EntryDetailsStrings.ViewModifications, "Versions", new { id = Model.Id }, new { id = "viewVersions" })
	<a href="#" id="reportEntryLink" data-bind="click: reportViewModel.show">@ViewRes.EntryDetailsStrings.ReportAnError</a>
	@Helpers.EntryStatusMessage(Model.Status)
}

@helper NicoTagLink(string nicoTag) {<a href="http://www.nicovideo.jp/tag/@nicoTag" class="extLink">@nicoTag</a>}

@if (Model.Deleted) {
	@EntryDetailsHelpers.DeletedBanner(null)
}

<div class="well well-transparent">

	<div class="media">
		@if (!string.IsNullOrEmpty(smallThumbUrl)) {
			<a class="pull-left" href="@thumbUrl">
				<img class="media-object" src="@smallThumbUrl" alt="Thumb" />
			</a>
		}

		<div class="media-body">
			@if (!Model.Description.IsEmpty) {
				<div class="entry-description" data-bind="with: description">
					@{ Html.RenderPartial("Partials/_EnglishTranslatedString", new EnglishTranslatedStringViewModel(Model.Description, 2100, 2000)); }
				</div>
			}

			@if (Model.RelatedTags.Any()) {
				<p>@Res.RelatedTags: @TagHelpers.TagList(Model.RelatedTags, tooltip: true)</p>
			}

			@if (Model.WebLinks.Any()) {
				@EntryDetailsHelpers.ExternalLinksList(Model.WebLinks, false)
				<br />
			}
			
			@if (Model.MappedNicoTags.Any()) {
				<p>@Res.MappedTags: @Html.LinkListHtml(Model.MappedNicoTags, NicoTagLink)</p>
			}

			@if (Model.CategoryName != string.Empty) {
				<p>@Res.Category: @Model.CategoryName</p>
			}

			@if (!string.IsNullOrEmpty(Model.Translations)) {
				<p>@Res.Translations: @Model.Translations</p>
			}

			@if (!string.IsNullOrEmpty(Model.AdditionalNames)) {
				<p>
					@Res.Aliases: @Model.AdditionalNames
				</p>
			}

			@if (related.Any()) {
				<div id="hierarchyContainer" style='width: 1000px; height: @((75 + (Model.Parent != null ? 125 : 0) + (Model.Children.Any() ? 125 : 0)) + "px")'></div>
				<br />
			}
			
			@if (Model.Targets != TagTargetTypes.Nothing && Model.Targets != TagTargetTypes.All) {
				<p>
					@Res.ValidFor: @string.Join(", ", EnumVal<EntryType>.Values.Where(e => e != EntryType.Undefined && Model.Targets.HasFlag((TagTargetTypes)e)))
				</p>
			}
			
			<p>@string.Format(Res.FollowCount, Model.FollowerCount)</p>

			<p>@ViewRes.EntryDetailsStrings.AdditionDate: @Helpers.UniversalTimeLabel(Model.CreateDate)</p>

			<br />

			<div>
				@Html.ActionLink(Res.AllEntries + " (" + (Model.ArtistCount + Model.AlbumCount + Model.SongCount + Model.EventCount) + ")", "Index", "Search", UrlMapper.Search.Entries(tagId: Model.Id), null)
			</div>

			@if (Model.ArtistCount > 0) {
				<div>
					@Html.ActionLink(Res.AllArtists + " (" + Model.ArtistCount + ")", "Index", "Search", UrlMapper.Search.Artists(tagId: Model.Id), null)
				</div>
			}

			@if (Model.AlbumCount > 0) {
				<div>
					@Html.ActionLink(Res.AllAlbums + " (" + Model.AlbumCount + ")", "Index", "Search", UrlMapper.Search.Albums(tagId: Model.Id), null)
				</div>
			}

			@if (Model.SongCount > 0) {
				<div>
					@Html.ActionLink(Res.AllSongs + " (" + Model.SongCount + ")", "Index", "Search", UrlMapper.Search.Songs(tagId: Model.Id), null)
				</div>
			}
			
			@if (Model.EventCount > 0) {
				<div>
					@Html.ActionLink(Res.AllEvents + " (" + Model.EventCount + ")", "Index", "Search", UrlMapper.Search.Events(tagId: Model.Id), null)
				</div>
			}
		</div>
	</div>
</div>

@if (Model.Artists.Any()) {
	<div class="well well-transparent">
		@Helpers.ShowMore(href: Url.Action("Index", "Search", UrlMapper.Search.Artists(tagId: Model.Id)))
		<h3>
			<a href="@Url.Action("Index", "Search", UrlMapper.Search.Artists(tagId: Model.Id))">
				@Res.TopArtists
				<small>(@Model.ArtistCount @SharedRes.Total)</small>
			</a>
		</h3>
		@ArtistHelpers.ArtistGrid(Model.Artists.Select(a => a), 2, true)
	</div>
}

@if (Model.Albums.Any()) {
	<div class="well well-transparent">
		@Helpers.ShowMore(href: Url.Action("Index", "Search", UrlMapper.Search.Albums(tagId: Model.Id)))
		<h3>
			<a href="@Url.Action("Index", "Search", UrlMapper.Search.Albums(tagId: Model.Id))">
				@Res.TopAlbums
				<small>(@Model.AlbumCount @SharedRes.Total)</small>
			</a>
		</h3>
		@AlbumHelpers.AlbumGrid(Model.Albums.Select(a => a), 2, false, false, true)
	</div>
}

@if (Model.Songs.Any()) {
	<div class="well well-transparent">
		@Helpers.ShowMore(href: Url.Action("Index", "Search", UrlMapper.Search.Songs(tagId: Model.Id)))
		<h3>
			<a href="@Url.Action("Index", "Search", UrlMapper.Search.Songs(tagId: Model.Id))">
				@Res.TopSongs
				<small>(@Model.SongCount @SharedRes.Total)</small>
			</a>
		</h3>
		@SongHelpers.SongGrid(Model.Songs.Select(s => s), 2, true)
	</div>
}


@if (Model.EventSeries.Any()) {
	<div class="well well-transparent">
		@Helpers.ShowMore(href: Url.Action("Index", "Search", UrlMapper.Search.Events(tagId: Model.Id)))
		<h3>
			<a href="@Url.Action("Index", "Search", UrlMapper.Search.Events(tagId: Model.Id))">
				@Res.EventSeries
			</a>
		</h3>
		@Helpers.EventSeriesThumbs(Model.EventSeries)
	</div>
}

@if (Model.Events.Any()) {
	<div class="well well-transparent">
		@Helpers.ShowMore(href: Url.Action("Index", "Search", UrlMapper.Search.Events(tagId: Model.Id)))
		<h3>
			<a href="@Url.Action("Index", "Search", UrlMapper.Search.Events(tagId: Model.Id))">
				@Res.TopEvents
				<small>(@Model.EventCount @SharedRes.Total)</small>
			</a>
		</h3>
		@Helpers.EventThumbs(Model.Events, ImageSize.TinyThumb)
	</div>
}

@if (related.Any()) {
	<div class="well well-transparent">
		@if (Model.Siblings.Any()) {
			<p>@Res.Siblings: @TagHelpers.TagList(Model.Siblings)</p>
		}
		@if (Model.Children.Any()) {
			<p>@Res.Children: @TagHelpers.TagList(Model.Children)</p>
		}
	</div>
}

<div id="tabs" class="js-cloak" data-bind="show, with: comments">

	<ul>
		<li><a href="#latestCommentsTab">@ViewRes.EntryDetailsStrings.LatestComments</a></li>
		<li data-tab="Discussion">
			<a href="#discussionTab">@ViewRes.EntryDetailsStrings.DiscussionTab (@Model.CommentCount)</a>
		</li>
	</ul>

	<div id="latestCommentsTab">
		@CommentHelpers.EditableComments(UserContext.HasPermission(PermissionToken.CreateComments), well: false, commentsBinding: "topComments", newCommentRows: 3, pagination: false)
		<!-- ko ifnot: comments().length -->
		<p>@ViewRes.EntryDetailsStrings.NoComments</p>
		<!-- /ko -->
		<p>
			<a href="#" id="viewCommentsLink">@ViewRes.EntryDetailsStrings.ViewAllComments</a>
		</p>
	</div>

	<div id="discussionTab">
		@CommentHelpers.EditableComments(UserContext.HasPermission(PermissionToken.CreateComments), well: false)
	</div>

</div>

@EntryDetailsHelpers.ReportEntryPopupKnockout()